﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;

public partial class Main_Reports_Accounts_CreditNote : System.Web.UI.Page
{
    Datautility DU = new Datautility();
    AccountSettings ac = new AccountSettings();
    double dblTotalDebit = 0;
    double dblTotalCredit = 0;
    string priveousrow = string.Empty;
    int firstrow = 0;

    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["UserId"] == null)
        {
            Response.Redirect("~/SessionExpired.aspx");
        }
        if (!IsPostBack)
        {
            lblcompname.Text = Session["CompName"].ToString();
            lblcompname.Font.Bold = true;
            hidFinyear.Value = Session["FinYear"].ToString();
          
            txtFromDate.Text = "01/04/" + Session["FinYear"].ToString();
            txtToDate.Text = "31/03/" +( Convert.ToInt32(Session["FinYear"].ToString()) + 1).ToString();
            getJournalBooks(txtFromDate.Text, txtToDate.Text);
        }
        
        //Response.Write(txtFromDate.Text);
        //Response.Write(txtToDate.Text);
         
    }
    protected void getJournalBooks(string strFromDate, string strToDate)
    {
        if (strFromDate != string.Empty && strToDate != string.Empty)
        {
            string[] str = strFromDate.Split('/');
            strFromDate= str[1]+'/'+str[0]+'/'+ str[2];
            str = strToDate.Split('/');
            strToDate = str[1] + '/' + str[0] + '/' + str[2];
            SqlParameter[] pr = { new SqlParameter("@FromDate",strFromDate),
                                 new SqlParameter("@ToDate",strToDate),
                                  new SqlParameter("@TransactionTypeList","2")
                               
                            };
            lblheading.Text = "Credit Note Books From " +string.Format("{0:dd-MMM-yyyy}",Convert.ToDateTime( strFromDate ))+ " To " + string.Format("{0:dd-MMM-yyyy}", Convert.ToDateTime(strToDate));
            
            DataTable dt = DU.getdatausingprocedure("USP_ACC_AccountBookVoucherDetails", pr);
            
            grddisplay.DataSource = dt;

            grddisplay.DataBind();
        }
    }
    protected void grddisplay_rowdatabound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            if (e.Row.Cells[4].Text != string.Empty)
            {
                dblTotalDebit += Math.Abs(Convert.ToDouble(e.Row.Cells[4].Text));
            }
            if (Math.Abs(Convert.ToDouble(e.Row.Cells[4].Text)) != 0)
            {
                e.Row.Cells[4].Text = ac.GetCurrency(Math.Abs(Convert.ToDouble(e.Row.Cells[4].Text))).ToString();
            }
            else
            {
                e.Row.Cells[4].Text = string.Empty;
            }



            if (e.Row.Cells[5].Text != string.Empty)
            {
                dblTotalCredit += Math.Abs(Convert.ToDouble(e.Row.Cells[5].Text));
            }
            if (Math.Abs(Convert.ToDouble(e.Row.Cells[5].Text)) != 0)
            {
                e.Row.Cells[5].Text = ac.GetCurrency(Math.Abs(Convert.ToDouble(e.Row.Cells[5].Text))).ToString();
            }
            else
            {
                e.Row.Cells[5].Text = string.Empty;
            }
        }
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            DataRowView dv = ((DataRowView)e.Row.DataItem);
            if (priveousrow == dv["TransactionNumber"].ToString())
            {
                if (grddisplay.Rows[firstrow].Cells[2].RowSpan == 0)
                {
                    grddisplay.Rows[firstrow].Cells[2].RowSpan = 2;
                }
                else
                {
                    grddisplay.Rows[firstrow].Cells[2].RowSpan += 1;
                }
                if (grddisplay.Rows[firstrow].Cells[1].RowSpan == 0)
                {
                    grddisplay.Rows[firstrow].Cells[1].RowSpan = 2;
                }
                else
                {
                    grddisplay.Rows[firstrow].Cells[1].RowSpan += 1;
                }
                if (grddisplay.Rows[firstrow].Cells[0].RowSpan == 0)
                {
                    grddisplay.Rows[firstrow].Cells[0].RowSpan = 2;
                }
                else
                {
                    grddisplay.Rows[firstrow].Cells[0].RowSpan += 1;
                }
                e.Row.Cells.RemoveAt(2);
                 e.Row.Cells.RemoveAt(1);
                 e.Row.Cells.RemoveAt(0);
            }
            else 
            {
                e.Row.VerticalAlign = VerticalAlign.Middle;
                priveousrow = dv["TransactionNumber"].ToString();
                firstrow = e.Row.RowIndex;
            }
         
        }
        if (e.Row.RowType == DataControlRowType.Footer)
        {
            e.Row.Cells[4].Text = ac.GetCurrency(dblTotalDebit).ToString();
            e.Row.Cells[5].Text = ac.GetCurrency(dblTotalCredit).ToString();
            e.Row.Cells[3].Text = "Total";
            e.Row.HorizontalAlign = HorizontalAlign.Right;
            e.Row.Font.Bold = true;

        }
    }
    protected void btnShow_Click(object sender, EventArgs e)
    {

        getJournalBooks(txtFromDate.Text, txtToDate.Text);

    }
    
  
}
